﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Brain.Rendering
{
    public class DefaultCameraManager : ICameraManager
    {
        public void DeserializedAll()
        {
        }
        public ICamera3D Camera3D { get; set; }

        public ICamera2D Camera2D { get; set; }

        public void Reset()
        {
            Camera2D = null;
            Camera3D = null;
        }

        public SerializationData Data { get; set; }

        public void SerializeData(DefaultSerializationManager manager)
        {
            Data = new SerializationData();
            if (Camera2D != null)
            {
                Data.AddData("Camera2D.Name", Camera2D.Name);
            }
            if (Camera3D != null)
            {
                Data.AddData("Camera3D.Name", Camera3D.Name);
            }
        }

        public void DeserializeData(DefaultSerializationManager manager, SerializationData newData)
        {
            string cameraName = newData.GetData<string>("Camera2D.Name");
            for (int i = 0; i < Engine.Instance.ScreenManager.Count(); i++)
            {
                GameScreen gs = Engine.Instance.ScreenManager.GetScreen(i);
                for (int x = 0; x < gs.AllComponents.Count; x++)
                {
                    IBaseComponent comp = gs.AllComponents[x];
                    if (comp is ICamera2D && comp.Name == cameraName)
                    {
                        this.Camera2D = comp as ICamera2D;
                        break;
                    }
                }
            }

            string camera3DName = newData.GetData<string>("Camera3D.Name");
            for (int i = 0; i < Engine.Instance.ScreenManager.Count(); i++)
            {
                GameScreen gs = Engine.Instance.ScreenManager.GetScreen(i);
                for (int x = 0; x < gs.AllComponents.Count; x++)
                {
                    IBaseComponent comp = gs.AllComponents[x];
                    if (comp is ICamera3D && comp.Name == camera3DName)
                    {
                        this.Camera3D = comp as ICamera3D;
                        break;
                    }
                }
            }
        }
    }
}
